﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UseCase.Dialogs.cs" company="">
//   
// </copyright>
// <summary>
//   The use case.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Libium.UseCases
{
    using System.IO;

    using Libium.Core;
    using Libium.Properties;

    /// <summary>
    ///   The use case.
    /// </summary>
    public abstract partial class UseCase
    {
        #region Methods

        /// <summary>
        /// The execute open file.
        /// </summary>
        /// <param name="filters">
        /// The filters. 
        /// </param>
        /// <param name="filterIndex">
        /// The filter index. 
        /// </param>
        /// <param name="initialDirectoryPath">
        /// The initial directory path. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        protected string ExecuteOpenFile(FileFilter[] filters, int filterIndex = 0, string initialDirectoryPath = null)
        {
            // TODO null???
            if (filters == null)
            {
                filters = new[] { FileFilters.All };
            }

            // TODO Replace settings
            if (initialDirectoryPath == null)
            {
                initialDirectoryPath = Settings.Default.FileUseCaseLastDirectory;
            }

            var useCase = new OpenFileUseCase(null, this.Service)
                {
                   InitialDirectoryPath = initialDirectoryPath, Filters = filters, FilterIndex = filterIndex 
                };

            this.Service.ExecuteModalUseCase(useCase);

            if (useCase.FilePath != null)
            {
                Settings.Default.FileUseCaseLastDirectory = Path.GetDirectoryName(useCase.FilePath);
            }

            return useCase.FilePath;
        }

        /// <summary>
        /// The execute open file.
        /// </summary>
        /// <param name="filter">
        /// The filter. 
        /// </param>
        /// <param name="filterIndex">
        /// The filter index. 
        /// </param>
        /// <param name="initialDirectoryPath">
        /// The initial directory path. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        protected string ExecuteOpenFile(FileFilter filter, int filterIndex = 0, string initialDirectoryPath = null)
        {
            return ExecuteOpenFile(filter == null ? null : new[] { filter }, filterIndex, initialDirectoryPath);
        }

        /// <summary>
        /// The execute save file.
        /// </summary>
        /// <param name="fileName">
        /// The file name. 
        /// </param>
        /// <param name="filters">
        /// The filters. 
        /// </param>
        /// <param name="filterIndex">
        /// The filter index. 
        /// </param>
        /// <param name="initialDirectoryPath">
        /// The initial directory path. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        protected string ExecuteSaveFile(
            string fileName, FileFilter[] filters, int filterIndex = 0, string initialDirectoryPath = null)
        {
            // TODO null???
            if (filters == null)
            {
                filters = new[] { FileFilters.All };
            }

            // TODO Replace settings
            if (initialDirectoryPath == null)
            {
                initialDirectoryPath = Settings.Default.FileUseCaseLastDirectory;
            }

            var useCase = new SaveFileUseCase(null, this.Service)
                {
                    FileName = fileName, 
                    InitialDirectoryPath = initialDirectoryPath, 
                    Filters = filters, 
                    FilterIndex = filterIndex
                };

            this.Service.ExecuteModalUseCase(useCase);

            if (useCase.FilePath != null)
            {
                Settings.Default.FileUseCaseLastDirectory = Path.GetDirectoryName(useCase.FilePath);
            }

            return useCase.FilePath;
        }

        /// <summary>
        /// The execute save file.
        /// </summary>
        /// <param name="fileName">
        /// The file name. 
        /// </param>
        /// <param name="filter">
        /// The filter. 
        /// </param>
        /// <param name="filterIndex">
        /// The filter index. 
        /// </param>
        /// <param name="initialDirectoryPath">
        /// The initial directory path. 
        /// </param>
        /// <returns>
        /// The <see cref="string"/> . 
        /// </returns>
        protected string ExecuteSaveFile(
            string fileName, FileFilter filter, int filterIndex = 0, string initialDirectoryPath = null)
        {
            return ExecuteSaveFile(
                fileName, filter == null ? null : new[] { filter }, filterIndex, initialDirectoryPath);
        }

        #endregion
    }
}